<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/14
 * Time: 17:38
 */

namespace App\Http\Controllers\V1;


use App\Http\Controllers\Controller;
use App\Providers\UploadFile;

class UploadController extends Controller
{
    public function index()
    {

        $upload = new UploadFile();

        $upload->maxSize  = 8*pow(2,20) ;// 设置附件上传大小  8M    默认为2M

        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg', 'pdf');// 设置附件上传类型   默认为空不检测扩展

        $key = key($_FILES);

        $pathinfo = pathinfo($_FILES[$key]['name']);

        $upload->savePath =  $pathinfo['extension'] == 'pdf' ? './files/' : './pictures/';// 设置附件上传目录   默认上传目录为 ./uploads/

        if(!$upload->upload()) {

            $this->sendJson([], 403, $upload->getErrorMsg());
        }else{

            $info =  $upload->getUploadFileInfo();

            $this->sendJson(['info' => $info]);
        }

    }

    public function file()
    {
        $upload=new UploadFile();
        $upload->maxSize  = 10*pow(2,20) ;// 设置附件上传大小  10M    默认为2M
        $upload->allowExts  = array('txt', 'doc', 'docx', 'xls', 'xlsx', 'rtf', 'pdf');// 设置附件上传类型   默认为空不检测扩展
        $upload->savePath =  '../public/files/';// 设置附件上传目录   默认上传目录为 ./uploads/

        if(!$upload->upload()) {
            // 上传错误提示错误信息
            $this->sendJson([], 403, $upload->getErrorMsg());
        }else{
            // 上传成功 获取上传文件信息
            $info =  $upload->getUploadFileInfo();
            $this->sendJson(['info' => $info]);
        }
    }

}